//地图url
let gisapiUrl = "http://120.77.223.190:9000/gisapi"
//服务器url

let baseUrl = "http://120.78.144.153:8080/RoadPlatform/"

// let baseUrl = "http://localhost:8080"
// let baseUrl = "http://120.78.144.153:8080/RoadPlatform"
// >>>>>>> ef6f61041dd529ea02f6bda3387139b760e253dc

//封装ajax,根据后台接收和发送的数据格式修改data的格式
//success和fail是成功/失败回调函数
function ajax(url = "", data = {}, type = "GET", success, fail) {
  //构造数据
  if(typeof data == 'object'){
    var str = '';
    for(var key in data){
        str += key+'='+data[key]+'&';
    }
    str = str.replace(/&$/, '');
  }
  //拼接url
  type = type.toUpperCase()
  url = baseUrl + url
  let requestObj
  if (window.XMLHttpRequest) {
    requestObj = new XMLHttpRequest()
  } else {
    requestObj = new ActiveXObject()
  }
  if (type === 'GET') {
    var str = '';
    if(typeof data == 'object'){
      for(var key in data){
          str += key+'='+data[key]+'&';
      }
      str = str.replace(/&$/, '');
    }
    if (str !== '') {
      url = url + '?' + str
    }
  }
  let sendData = ''
  if (type === 'POST' || type === 'PATCH' || type === 'DELETE') {
    sendData = data
  }
  requestObj.open(type, url, true)
  // requestObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  requestObj.setRequestHeader("Content-Type", "application/json")
  requestObj.send(JSON.stringify(sendData))
  requestObj.onreadystatechange = () => {
    if (requestObj.readyState === 4) {
      if (requestObj.status === 200) {
        let obj = requestObj.responseText
		success(obj)
      } else {
        fail(requestObj)
      }
    }
  }
}

// function getValue(obj){
// 	var res = obj
// }

//与后台的交互方法写在这里↓↓
let service = {
  createUser: () => {
    ajax('/users/', {id : 6, name: 'sby', age: 24}, 'POST', console.log, console.log)
  },
  getAllUsers: () => {
    ajax('/users/', {}, 'GET',console.log,console.log)
  },
  getRegion: () => {
	ajax('/region', {}, "GET", success, fail)
  },
  
  editRegion: () => {
     ajax('/region', {data: [{
      id: 241,
      regionCode:1555555,
      regionName:"上海市111",
      levelCode:0,
      pregionCode:0}]}, 'PATCH', console.log, console.log)
  },
  
  //按页获得区域
  getRegionBypage: (page, size, success, fail) => {
	ajax('/region/bypage?page='+page+'&size='+size, {}, "GET", success, fail)
  },
  //删除Region
  deleteRegion: (_id, success, fail) => {
    ajax('/region', {data: [{
      id: _id,
    }]}, 'delete', success, fail)
  },
  //新增Region
  addRegion: (_regionCode, _regionName, _levelCode, _pregionCode, success, fail) => {
    ajax('/region', {data: [{
	  regionCode: 	_regionCode,
	  regionName: 	_regionName,
      levelCode:	_levelCode,
      pregionCode:	_pregionCode}]
	}, 'POST', success, fail)
  },
  //按页获得单位
  getCompanyBypage: (page, size, name, success, fail) => {
	ajax('/company/bypage?page='+page+'&size='+size+'&name='+name, {}, "GET", success, fail) 
  },
  //新增Company
  addCompany: (_companyCode, _companyName, _levelCode, _pcompanyCode, _regionCode, success, fail) => {
    ajax('/company', {data: [{
	  companyCode: 	_companyCode,
	  companyName: 	_companyName,
      levelCode:	_levelCode,
	  pcompanyCode: _pcompanyCode,
      region:	{
		id: _regionCode
		}
	  }]
	}, 'POST', success, fail)
  },
  //删除Company
  deleteCompany: (_id, success, fail) => {
    ajax('/company', {data: [{
      id: _id,
    }]
	}, 'delete', success, fail)
  },
  //修改Company
  patchCompany: (_id, _companyCode, _companyName, _levelCode, _pcompanyCode, _regionCode, success, fail) => {
    ajax('/company', {data: [{
	  id:			_id,
	  companyCode: 	_companyCode,
	  companyName: 	_companyName,
      levelCode:	_levelCode,
	  pcompanyCode: _pcompanyCode,
      region:	{
		id: _regionCode
		}
	  }]
	}, 'PATCH', success, fail)
  },
  
  //按页获得role
  getRoleBypage: (page, size, success, fail) => {
	ajax('/role/bypage?page='+page+'&size='+size, {}, "GET", success, fail) 
  },
  //获得角色权限
  getPermission: (roleId, success, fail) =>{
	ajax('/role/'+roleId+'/permissions', {}, "GET", success, fail) 
  },
  //增加角色
  addRole: ( _code, _name, _level, _remark, success, fail) => {
    ajax('/role', {data: [{
	  roleCode: 	_code,
	  roleName: 	_name,
      childP:		_level,
	  remark: 		_remark
	  }]
	}, 'POST', success, fail)
  },
  //修改角色
  modifyRole: (_id, _code, _name, _level, _remark, success, fail) => {
	  ajax('/role/',{data: [{
	  id:			_id,
	  roleCode: 	_code,
	  roleName: 	_name,
      childP:		_level,
	  remark:		_remark
	  }]
	}, 'PATCH', success, fail)
  },
  //修改角色权限
  modifyRolePermission: (id, js, success, fail) => {
	  ajax('/role/'+ id + '/managepermissions',{data:js}, 'POST', success, fail)
  },
  //删除角色
  deleteRole: (_id, success, fail) => {
    ajax('/role', {data: [{
      id: _id,
    }]
	}, 'delete', success, fail)
  },
  
  //按页获得personnel
  getPersonnelBypage: (page, size, name, success, fail) => {
	ajax('/personnel/bypage?page='+page+'&size='+size+'&name='+name, {}, "GET", success, fail) 
  },
  //修改用户
  modifyPersonnel: (_id, _code, _passwd, _name, _pcode, _tel, _role, success, fail) => {
	ajax('/personnel',{data: [{
	  id:					_id,
	  personnelCode: 		_code,
	  personnelPassword: 	_passwd,
      postName:				_name,
	  tel:					_tel,
	  roleCode:				_role,
	  company:{
			id: _pcode
		}
	  }]
	}, 'PATCH', success, fail)
  },
   //增加用户
  addPersonnel: (_code, _passwd, _name, _pcode, _tel, _role, success, fail) => {
	ajax('/personnel',{data: [{
	  personnelCode: 		_code,
	  personnelPassword: 	_passwd,
      postName:				_name,
	  tel:					_tel,
	  roleCode:				_role,
	  company:{
			id: _pcode
		}
	  }]
	}, 'POST', success, fail)
  },
  //查看用户单独的权限
  getPersonnelPermission: (_pid, success, fail) => {
	  ajax('/personnel/'+_pid+'/permissions', {}, "GET", success, fail) 
  },
  //修改用户单独的权限
  modifyPersonnelPermission: (_pid, js, success, fail) => {
	  ajax('/personnel/'+ _pid + '/managepermissions',{data:js}, 'POST', success, fail)
  },
  //删除用户
    deletePersonnel: (_id, success, fail) => {
    ajax('/personnel', {data: [{
      id: _id,
    }]
	}, 'delete', success, fail)
  }
}

//本地测试
function test() {
  
  // service.getRegion()
  // service.getRegion()

}

test()  